const app = getApp()
Page({
	/* 页面的初始数据*/
	data: {
        phoneShow: false,//手机号登陆弹框
        phone: '',//手机号
        verifyCode:'',//验证码
        sessionKey:'',
        wxOpenId:'',
        borderNone: false,//不显示border
        facePic: '',//用户头像
        gender: '',//用户性别
        get_code_msg: '获取验证码',//验证码显示信息
        sendmessageCode: true,//是否可以点击获取验证码
        companyList:[
            {name:'山西旺龙',logoUrl:'/images/login/logo1.png'},
            {name:'吉林正和',logoUrl:'/images/login/logo2.png'},
            {name:'贵州神奇',logoUrl:'/images/login/logo3.png'},
            {name:'松鹿制药',logoUrl:'/images/login/logo4.png'},
            {name:'长春人民',logoUrl:'/images/login/logo5.png'},
            {name:'孟和药业',logoUrl:'/images/login/logo6.png'},
            {name:'吉林吉春',logoUrl:'/images/login/logo7.png'},
            {name:'辅仁药业',logoUrl:'/images/login/logo8.png'},
            {name:'神威药业',logoUrl:'/images/login/logo9.png'},
            {name:'东阿阿胶',logoUrl:'/images/login/logo10.png'},
            {name:'香雪制药',logoUrl:'/images/login/logo11.png'},
            {name:'丽珠集团',logoUrl:'/images/login/logo12.png'},
            {name:'振东集团',logoUrl:'/images/login/logo13.png'},
            {name:'太极集团',logoUrl:'/images/login/logo14.png'},
            {name:'江苏康缘',logoUrl:'/images/login/logo15.png'},
        ],//招募企业列表
	},
	onLoad() {
        /*获取 wxOpenId sessionKey */
        this.setData({
            wxOpenId:wx.getStorageSync('wxOpenId'),
            sessionKey:wx.getStorageSync('sessionKey')
        })
	},
    /* 显示手机号登陆*/
    phoneShow () {
        this.setData({
            phoneShow :true,
            phone : '',
            verifyCode : ''
        })
    },
    phonenClose () {
        this.setData({ phoneShow:false })
    },
    /*设置字段*/
    setField(event){
        let field = event.target.dataset.field;
        let obj = {};
        obj[field] = event.detail;
        this.setData(obj)
    },
    /* 发送验证码 */
    sendCode () {
        let _this = this;
        if (!this.data.sendmessageCode) {//已经发送过验证码了
            return
        }
        if (!this.data.phone) {
            app.dialogJs.tip('请输入手机号');
            return
        }
        if (this.data.phone.length != 11) {
            app.dialogJs.tip('请输入正确的手机号');
            return
        }
        app.getRequest('/login/verifyCode', { tel:_this.data.phone }).then(res => {
            if (res.code == 0) {
                app.dialogJs.tip(res.message);
                let wait=60;
                let time = function() {
                    if (wait == 0) {
                        _this.setData({
                            sendmessageCode: true,
                            get_code_msg: '获取验证码'
                        });
                        wait = 59;
                    } else {
                        _this.setData({
                            sendmessageCode: false,
                            get_code_msg: wait + '秒后重新发送'
                        });
                        wait--;
                        setTimeout(function() {
                            time()
                        }, 1000)
                    }
                };
                time();
            } else {
                app.dialogJs.tip(res.message)
            }
        })
    },
    /* 手机号登录 */
    phoneLogin () {
        let params = {
            tel: this.data.phone,
            verifyCode: this.data.verifyCode,
            wxOpenId: this.data.wxOpenId,
            sessionKey: this.data.sessionKey,
            facePic: this.data.facePic,
            gender: this.data.gender,
        };
        if(!params.tel||!params.verifyCode){
            app.dialogJs.tip("请输入手机号和验证码")
        }else{
            app.postRequest('/login/checkTelCode', params).then(res => {
                if (res.code == 0) {
                    if(res.data && res.data.updateStatus == 1){//跳查看信息页
                        app.utilJs.jumpTo('/pages/userDetail/userDetail')
                    }else{
                        app.utilJs.jumpTo('/pages/info/info')
                    }
                }else {
                    app.dialogJs.tip(res.message)
                }
                this.phonenClose();
            })
        }
    },
    /* 微信授权获取手机号 */
    getPhoneNumber(e){
        let that = this;
        if (e.detail.errMsg == "getPhoneNumber:ok") {
            let params = {
                wxOpenId:that.data.wxOpenId,
                sessionKey:that.data.sessionKey,
                facePic: that.data.facePic,
                gender: that.data.gender,
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv
            }
            app.postRequest('/login/register', params).then(res => {
                if (res.code == 0) {
                    if(res.data && res.data.updateStatus == 1){//跳查看信息页
                        app.utilJs.jumpTo('/pages/userDetail/userDetail')
                    }else{
                        app.utilJs.jumpTo('/pages/info/info')
                    }
                }else {
                    app.dialogJs.tip(res.message)
                }
            })
        }else{
            app.dialogJs.tip('您拒绝了手机号授权！')
        }
    },
    junpToProtocol(){
        app.utilJs.jumpTo('/pages/protocol/protocol')
    }
})
